package main

import (
	"context"
	"fmt"
	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/consumer"
	"github.com/apache/rocketmq-client-go/v2/primitive"
	"os"
	"time"
)

func main() {
	//创建一个新的消费者实例
	c, _ := rocketmq.NewPushConsumer(
		//消费组名称
		consumer.WithGroupName("testGroup"),
		consumer.WithNsResolver(primitive.NewPassthroughResolver([]string{"10.3.189.12:9876"})),
	)
	//订阅主题是test并设置消息选择器和回调函数
	err := c.Subscribe("test", consumer.MessageSelector{}, func(ctx context.Context,
		msgs ...*primitive.MessageExt) (consumer.ConsumeResult, error) {
		//遍历消息并打印每条消息
		for i := range msgs {
			fmt.Printf("打印消息: %v \n", msgs[i])
		}
		//返回消息处理成功的结果
		return consumer.ConsumeSuccess, nil
	})
	if err != nil {
		fmt.Println(err.Error())
	}
	// 启动消费者
	err = c.Start()
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(-1)
	}
	time.Sleep(time.Hour)
	//关闭消费者
	err = c.Shutdown()
	if err != nil {
		fmt.Printf("shutdown Consumer error: %error_count", err.Error())
	}
}
